<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\model;


use my\Define;
use my\Rsa;
use my\Security;
use think\Db;
use think\Model;

class DriverModel extends Model
{


    public function __construct()
    {
        parent::__construct();

    }

 /*   public function getUserByUid($uid, $string)
    {
        return Db::name("user")->field($string)->where(array('uid'=>$uid))->find();
    }

    public function getUserByPhone($phone, $string)
    {
        return Db::name("user")->field($string)->where(array('user_phone'=>Security::encrypt($phone)))->find();
    }

    public function createUser($phone)
    {
        $data['user_phone']=Security::encrypt(($phone));
        $data['user_pass']=md5(rand(10000000,90000000)."xmcx");
        $data['register_time']=time();
        $data['token']=md5(date("YmdHis",time()).rand(10000,99999));
        return Db::name('user')->insertGetId($data);
        //return Db::name('user')->getLastInsID();
    }

    public function UpdateUser($data)
    {
        Db::name('user')->update($data);
    }

    public function getSign($uid)
    {
        $where['record_time']=date('Ymd',time());
        $where['uid']=$uid;
        return Db::name("sign")->field("id")->where($where)->find();
    }

    public function sign($uid)
    {
        $data['record_time']=date('Ymd',time());
        $data['uid']=$uid;
        $data['add_time']=time();
        Db::name("sign")->insert($data);
    }*/

    public function getDriverByPhone($phone, $string)
    {
        return Db::name("driver_base")->field($string)->where(array('driver_phone'=>Security::encrypt($phone)))->find();
    }

    public function UpdateDriver($data)
    {
      //  return $data;
      return Db::name('driver_base')->update($data);
    }

    public function CreateDriver($driver_phone,$password)
    {
        $data['driver_phone']=Security::encrypt($driver_phone);
        $data['driver_pass']=md5($password);
        $data['register_time']=time();
        $data['auth_status']=0;//未认证
        //$data['token']=md5(date("YmdHis",time()).rand(10000,99999));
        return Db::name('driver_base')->insertGetId($data);
    }

    public function getDriverByDid($id,$string)
    {
        return Db::name("driver_base a")->join(['xm_driver_info'=>'b'], 'b.driver_id=a.driver_id', 'LEFT')->field($string)->where(['a.driver_id'=>$id])->find();
    }

    /**
     * 给司机的余额修改和给司机添加一个行为数据
     *
     * @var string
     **/
    public function addDriveMoney($id,$money,$common_id){
        $res_wallet = Db::name("driver_money")->where(array('driver_id'=>$id))->setInc('money',$money);
        Db::name('driver_base')->where('driver_id',$id)->update(['driver_status'=>1]);
        $data['driver_id'] = $id;
        $data['money'] = $money;
        $data['time'] = time();
        $data['record_type'] = 1;
        $data['common_id'] = $common_id;
        $res_wallet_record = Db::name('driver_wallet_record')->insert($data);
        if($res_wallet && $res_wallet_record){
            return true;
        }
    }

    public function getWorkArea($did,$string)
    {
        if(!in_array($string,array_keys(config('work_area')))){
            $string = 'work_province';
        }
        return Db::name('driver_base a')->join('region b','b.region_id=a.'.$string,'LEFT')
            ->where('a.driver_id='.$did)->value('region_name');
    }

    public function getAuthFailReason($map=array())
    {
        return Db::name('driver_auth_record')->where($map)->order('record_id desc')->column('reason');
    }

}